#include <iostream>
using namespace std;

int GetMonthDay(int year, int month)
{

    static int monthDayArray[13] = { -1, 31, 28, 31, 30, 31, 30,
31, 31, 30, 31, 30, 31 };

    if (month == 2 && ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)))
    {
        return 29;
    }

    return monthDayArray[month];
}


void Solution(int year, int month, int day, int add) {
    day += add;
    while (day > GetMonthDay(month, day)) {
        day -= GetMonthDay(month, day);
        month++;
        if (month == 13) {
            year += 1;
            month = 1;
        }
    }


    cout << year << "-" << month << "-" << day << endl;
}

int main() {
    int year, month, day = 0;
    int n, add = 0;
    cin >> n;
    for (int i = 0; i < n; i++) {
        cin >> year >> month >> day >> add;
        Solution(year, month, day, add);
    }

}